﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using OutlookContactsSync.Domain.Presenters;
using OutlookContactsSync.Domain.Properties;
using OutlookContactsSync.Domain.Views;
using OutlookContactsSync.Presenters;

namespace OutlookContactsSync
{
    public sealed partial class MainForm : BaseForm, IOutlookSyncMainView   
    {
        public MainForm()
        {
            InitializeComponent();

            Text = Resources.MainFormTitle;
            titleLabel.Text = Resources.SynchronizeOutlookContacts;
            statusLinkLabel.Text = Resources.viewApplication;
            ContactPicture = Resources.UnknownPicture;
            
            Syncing = false;
        }

        public override void ShowForm()
        {
            Application.Run(this);
        }

        #region IOutlookSyncMainView Members

        public event EventHandler StartSync;
        public event EventHandler CancelSync;

        public string ProgressMessage
        {
            set { progressLabel.Text = value; }
        }

        public string CurrentContactName
        {
            set { contactNameLabel.Text = value; }
        }

        public int Progress
        {
            set { progressBar.Value = value; }
        }

        public string ProgressCounter
        {
            set
            {
                labelProgressCounter.Text = value;
            }
        }

        public bool ShowProgressCounter
        {
            set
            {
                labelProgressCounter.Visible = value;
            }
        }

        public bool Syncing
        {
            set
            {
                startButton.Enabled = !value;
                settingsLinkLabel.Enabled = !value;
                statusLinkLabel.Enabled = !value;
                cancelButton.Enabled = value;
            }
        }

        public System.Drawing.Image ContactPicture
        {
            set
            {
                contactPictureBox.Image = value;
            }
            get
            {
                return contactPictureBox.Image;
            }
        }

        #endregion

        private void StartButtonClick(object sender, EventArgs e)
        {
            Syncing = true;
            ContactPicture = Resources.syncing;

            if (StartSync != null)
            {
                StartSync(this, EventArgs.Empty);
            }
        }

        private void SettingsLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            IOutlookSyncSettingsPresenter outlookSyncSettingsPresenter = new OutlookSyncSettingsPresenter(new SettingsForm());
            outlookSyncSettingsPresenter.InitView();
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            if (CancelSync != null)
            {
                CancelSync(this, EventArgs.Empty);
            }
        }

        private void StatusLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            IOutlookSyncStatusPresenter outlookSyncStatusPresenter = new OutlookSyncStatusPresenter(new StatusForm());
            outlookSyncStatusPresenter.InitView();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Trace.WriteLine("App closed", "info");
        }
    }
}